<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\model;
class Maowenke
{
    /**递归删除文件夹
     * @param $url  要删除的文件地址
     * @return int  返回1是成功，返回0是失败
     */
    public function deleteDir($url){
        if(!is_dir($url)){
            return 0;
        }
        $array = scandir($url);
        foreach ($array as $value){
            if($value!='.'&&$value!='..'){
                if(is_dir($url.DS.$value)){
                    $this->deleteDir($url.DS.$value);
                }else{
                    unlink($url.DS.$value);
                }
            }
            $array = json_decode(file_get_contents(ROOT_PATH.'public'.DS.'uploads'.DS.'mignchen.txt'),true);
            if(count($array)&&isset($array[$value])){
                unset($array[$value]);
                file_put_contents(ROOT_PATH.'public'.DS.'uploads'.DS.'mignchen.txt',json_encode($array));
            }
        }
        rmdir($url);
        return 1;
    }
    static function datatoarray($data,$key,$value){
        $datas =[];
        foreach ($data as $values){
            $datas[$values[$key]] = $values[$value];
        }
        return $datas;
    }

    /**为title添加前缀
     * @param $data
     * @param $name
     * @param int $num
     * @param int $page
     * @return array
     */
    static function digui($data,$name,$num=0,$page=0){
        $array = [];
//        $str = '';
//        if($page>0){
//           $str = '|'.str_repeat('-',$page);
//        }
        foreach ($data as $key=>$value){
            if($value['pid']==$num){
//                $value[$name] = $str.$value[$name];
                $array[] = $value;
                unset($data[$key]);
                $arrays = self::digui($data,$name,$value['id'],$page+1);
                $array = array_merge($array,$arrays);
            }
        }
        return $array;
    }
}